package org.ttitfly.zuoyue.web.room;

import java.io.InputStream;
import java.io.ByteArrayInputStream;   
import java.io.ByteArrayOutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.asm.Type;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.encode.JsonBinder;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.ttitfly.ttcore.entity.account.User;
import org.ttitfly.ttcore.entity.system.Dictionary;
import org.ttitfly.ttcore.service.account.UserManager;
import org.ttitfly.ttcore.util.ObjectConverter;
import org.ttitfly.ttcore.util.enums.SequenceType;
import org.ttitfly.ttcore.util.helper.SequenceGenerator;
import org.ttitfly.ttcore.vo.RoomProjectTask;
import org.ttitfly.ttcore.web.CrudActionSupport;
import org.ttitfly.workflow.engine.run.Task;
import org.ttitfly.zuoyue.entity.attachment.Attachment;
import org.ttitfly.zuoyue.entity.complaint.Complaint;
import org.ttitfly.zuoyue.entity.room.RoomProject;
import org.ttitfly.zuoyue.entity.room.RoomTask;
import org.ttitfly.zuoyue.enums.RoomDataType;
import org.ttitfly.zuoyue.service.complaint.ComplaintManager;
import org.ttitfly.zuoyue.service.room.RoomProjectFlowManager;
import org.ttitfly.zuoyue.service.room.RoomProjectManager;
import org.ttitfly.zuoyue.service.workflow.TaskManager;

import com.google.gson.Gson;
import com.googlecode.jsonplugin.JSONException;
import com.googlecode.jsonplugin.JSONUtil;
import com.googlecode.jsonplugin.annotations.JSON;

public class RoomProjectAction extends CrudActionSupport<RoomProject> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Long id;

	private int processInstanceId;
	
	private Task task;
	
	private int taskId;
	
	private String actionName;
	
	private String bizNumber;
	
	private RoomProject roomProject;
	
	private String operateRemark;
	
	private Long attachmentId;
	
	private int start;
	
	private int limit;
	
	private String processName;
	
	private String activityName;
	
	private String processInstanceName;
	
	private Page<RoomTask> taskResult;
	
	@Resource
	private RoomProjectFlowManager roomProjectFlowManager;
	
	@Resource
	private RoomProjectManager roomProjectManager;
	
	@Resource
	private ComplaintManager complaintManager;
	
	@Resource
	private TaskManager taskManager;
	
	@Resource
	private UserManager userManager;
	
	@Resource
	private DataSource datasource;
	
	private String itemsJson;
	
	private List<Attachment> attachmentFiles;
	
	private String complaints;
	
	//修改项目使用
	private Long projectId;
	
	//修改项目使用
	List<Complaint> complaintRecords = new ArrayList<Complaint>();
	
	private Boolean submit;
	
	private Page<RoomProject> page = new Page<RoomProject>();
	
	private Boolean update = Boolean.FALSE;

	private Map<String, Object> rp = new HashMap<String, Object>();
	
	private String unitName;
	
	//下载辅助属性
	private InputStream excelStream;
	//下载的文件名
	private String fileName;
	//RoomProject和Task结合属性bean
	private RoomProjectTask rpt = new RoomProjectTask();
	
	private Page<RoomProjectTask> rptPage = new Page<RoomProjectTask>();
	
	//数据总行数
	private int totalCount;
	
	private String export;
	
	private String jsonString;
	
	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	//是否导出数据
	public String getExport() {
		return export;
	}

	public void setExport(String export) {
		this.export = export;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public RoomProjectTask getRpt() {
		return rpt;
	}

	public void setRpt(RoomProjectTask rpt) {
		this.rpt = rpt;
	}

	public Page<RoomProjectTask> getRptPage() {
		return rptPage;
	}

	public void setRptPage(Page<RoomProjectTask> rptPage) {
		this.rptPage = rptPage;
	}

	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getUnitName() {
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}


	public Map<String, Object> getRp() {
		return rp;
	}

	public void setRp(Map<String, Object> rp) {
		this.rp = rp;
	}

	public Page<RoomProject> getPage() {
		return page;
	}

	public void setPage(Page<RoomProject> page) {
		this.page = page;
	}

	public Page<RoomTask> getTaskResult() {
		return taskResult;
	}

	public void setTaskResult(Page<RoomTask> taskResult) {
		this.taskResult = taskResult;
	}

	public List<Attachment> getAttachmentFiles() {
		return attachmentFiles;
	}

	public void setAttachmentFiles(List<Attachment> attachmentFiles) {
		this.attachmentFiles = attachmentFiles;
	}

	public String getItemsJson() {
		return itemsJson;
	}
	
	public void setItemsJson(String itemsJson) {
		this.itemsJson = itemsJson;
	}
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	private RoomProject convertRoomProject(RoomProject rp){
		RoomProject rpNew = new RoomProject();
		rpNew.setId(rp.getId());
		rpNew.setBizNumber(rp.getBizNumber());
		rpNew.setName(rp.getName());
		rpNew.setAddress(rp.getAddress());
		rpNew.setTelphone(rp.getTelphone());
		rpNew.setLat(rp.getLat());
		rpNew.setLon(rp.getLon());
		rpNew.setNewCreate(rp.getNewCreate());
		rpNew.setRelatedCode(rp.getRelatedCode());
		rpNew.setSiteNumber(rp.getSiteNumber());
		rpNew.setSiteName(rp.getSiteName());
		rpNew.setBsc(rp.getBsc());
		rpNew.setLiftNumber(rp.getLiftNumber());
		rpNew.setUnderParkCarNumber(rp.getUnderParkCarNumber());
		rpNew.setUnderParkSignal(rp.getUnderParkSignal());
		rpNew.setUnderParkNumber(rp.getUnderParkNumber());
		rpNew.setCoverArea(rp.getCoverArea());
		rpNew.setResolveMethod(rp.getResolveMethod());
		rpNew.setLiftTopFloor(rp.getLiftTopFloor());
		rpNew.setPath(rp.getPath());
		rpNew.setRemark(rp.getRemark());
		rpNew.setEmergencyLevel(rp.getEmergencyLevel());
		rpNew.setNetType(rp.getNetType());
		rpNew.setResolveContent(rp.getResolveContent());
		rpNew.setMainSignal(rp.getMainSignal());
		rpNew.setLiftSignal(rp.getLiftSignal());
		rpNew.setArea(rp.getArea());
		rpNew.setAreaType(rp.getAreaType());
		rpNew.setProjectType(rp.getProjectType());
		rpNew.setGsmTerm(rp.getGsmTerm());
		rpNew.setTdTerm(rp.getTdTerm());
		rpNew.setLteTerm(rp.getLteTerm());
		rpNew.setWlanTerm(rp.getWlanTerm());
		rpNew.setGsmBuildType(rp.getGsmBuildType());
		rpNew.setTdBuildType(rp.getTdBuildType());
		rpNew.setLteBuildType(rp.getLteBuildType());
		rpNew.setWlanBuildType(rp.getWlanBuildType());
		rpNew.setGsmSiteNumber(rp.getGsmSiteNumber());
		rpNew.setGsmBsc(rp.getGsmBsc());
		rpNew.setTdSiteNumber(rp.getTdSiteNumber());
		rpNew.setTdRnc(rp.getTdRnc());
		rpNew.setLteSiteNumber(rp.getLteSiteNumber());
		rpNew.setLteBsc(rp.getLteBsc());
		rpNew.setWlanSiteNumber(rp.getWlanSiteNumber());
		rpNew.setWlanAc(rp.getWlanAc());
		rpNew.setRepeaterMaterial(rp.getRepeaterMaterial());
		rpNew.setRepeaterBuildFactory(rp.getRepeaterBuildFactory());
		rpNew.setRepeaterBuildCost(rp.getRepeaterBuildCost());
		rpNew.setRepeaterBudgetTotal(rp.getRepeaterBudgetTotal());
		rpNew.setHorsePowerSuit(rp.getHorsePowerSuit());
		rpNew.setItemFinishedTime(rp.getItemFinishedTime());
		rpNew.setTransferMaterialCost(rp.getTransferMaterialCost());
		rpNew.setFgysBuildConst(rp.getFgysBuildConst());
		rpNew.setFgysBuildUnit(rp.getFgysBuildUnit());
		rpNew.setFgysBudgetTotal(rp.getFgysBudgetTotal());
		rpNew.setTransferMaterial(rp.getTransferMaterial());
		rpNew.setAllTransferMaterialCost(rp.getAllTransferMaterialCost());
		//rpNew.setCivilCostBelong(rp.getCivilCostBelong());
		rpNew.setCivilInvestBelong(rp.getCivilInvestBelong());
		//rpNew.setBodycareCostBelong(rp.getBodycareCostBelong());
		rpNew.setBodycareInvestBelong(rp.getBodycareInvestBelong());
		//rpNew.setFgysCostBelong(rp.getFgysCostBelong());
		rpNew.setFgysInvestBelong(rp.getFgysInvestBelong());
		//rpNew.setRepeaterCostBelong(rp.getRepeaterCostBelong());
		rpNew.setRepeaterInvestBelong(rp.getRepeaterInvestBelong());
		rpNew.setFgysMisCode(rp.getFgysMisCode());
		rpNew.setRepeaterMisCode(rp.getRepeaterMisCode());
		rpNew.setHorsePowerMisCode(rp.getHorsePowerMisCode());
		
		rpNew.setWirePileLineNumber(rp.getWirePileLineNumber());
		rpNew.setWireDeviceNumber(rp.getWireDeviceNumber());
		rpNew.setWireLineMisCode(rp.getWireLineMisCode());
		rpNew.setWireDeviceMisCode(rp.getWireDeviceMisCode());
		return rpNew;
		
	}
	
	@SuppressWarnings("unused")
	private void copyRoomProject(RoomProject source,RoomProject target){
		target.setBizNumber(source.getBizNumber()==null?"":source.getBizNumber());
		target.setName(source.getName()==null?"":source.getName());
		target.setAddress(source.getAddress());
		target.setTelphone(source.getTelphone());
		target.setLat(source.getLat());
		target.setLon(source.getLon());
		target.setNewCreate(source.getNewCreate());
		target.setRelatedCode(source.getRelatedCode());
		target.setSiteNumber(source.getSiteNumber());
		target.setSiteName(source.getSiteName());
		target.setBsc(source.getBsc());
		target.setLiftNumber(source.getLiftNumber());
		target.setUnderParkCarNumber(source.getUnderParkCarNumber());
		target.setUnderParkSignal(source.getUnderParkSignal());
		target.setUnderParkNumber(source.getUnderParkNumber());
		target.setCoverArea(source.getCoverArea());
		target.setResolveMethod(source.getResolveMethod());
		target.setLiftTopFloor(source.getLiftTopFloor());
		target.setPath(source.getPath());
		target.setRemark(source.getRemark());
		target.setEmergencyLevel(source.getEmergencyLevel());
		target.setNetType(source.getNetType());
		target.setResolveContent(source.getResolveContent());
		target.setMainSignal(source.getMainSignal());
		target.setLiftSignal(source.getLiftSignal());
		target.setArea(source.getArea());
		target.setAreaType(source.getAreaType());
		target.setProjectType(source.getProjectType());
		target.setComplaint(source.getComplaint());
		target.setArea(source.getArea());
		target.setAreaType(source.getAreaType());
		target.setProjectType(source.getProjectType());
		target.setGsmTerm(source.getGsmTerm());
		target.setTdTerm(source.getTdTerm());
		target.setLteTerm(source.getLteTerm());
		target.setWlanTerm(source.getWlanTerm());
		target.setGsmBuildType(source.getGsmBuildType());
		target.setTdBuildType(source.getTdBuildType());
		target.setLteBuildType(source.getLteBuildType());
		target.setWlanBuildType(source.getWlanBuildType());
		target.setGsmSiteNumber(source.getGsmSiteNumber());
		target.setGsmBsc(source.getGsmBsc());
		target.setTdSiteNumber(source.getTdSiteNumber());
		target.setTdRnc(source.getTdRnc());
		target.setLteSiteNumber(source.getLteSiteNumber());
		target.setLteBsc(source.getLteBsc());
		target.setWlanSiteNumber(source.getWlanSiteNumber());
		target.setWlanAc(source.getWlanAc());
		
		target.setRepeaterMaterial(source.getRepeaterMaterial());
		target.setRepeaterBuildFactory(source.getRepeaterBuildFactory());
		target.setRepeaterBuildCost(source.getRepeaterBuildCost());
		target.setRepeaterBudgetTotal(source.getRepeaterBudgetTotal());
		target.setHorsePowerSuit(source.getHorsePowerSuit());
		target.setItemFinishedTime(source.getItemFinishedTime());
		target.setTransferMaterialCost(source.getTransferMaterialCost());
		target.setFgysBuildConst(source.getFgysBuildConst());
		target.setFgysBuildUnit(source.getFgysBuildUnit());
		target.setFgysBudgetTotal(source.getFgysBudgetTotal());
		target.setTransferMaterial(source.getTransferMaterial());
		target.setAllTransferMaterialCost(source.getAllTransferMaterialCost());

		//target.setCivilCostBelong(source.getCivilCostBelong());
		target.setCivilInvestBelong(source.getCivilInvestBelong());
		//target.setBodycareCostBelong(source.getBodycareCostBelong());
		target.setBodycareInvestBelong(source.getBodycareInvestBelong());
		//target.setFgysCostBelong(source.getFgysCostBelong());
		target.setFgysInvestBelong(source.getFgysInvestBelong());
		//target.setRepeaterCostBelong(source.getRepeaterCostBelong());
		target.setRepeaterInvestBelong(source.getRepeaterInvestBelong());
		target.setFgysMisCode(source.getFgysMisCode());
		target.setRepeaterMisCode(source.getRepeaterMisCode());
		target.setHorsePowerMisCode(source.getHorsePowerMisCode());
		
		target.setWirePileLineNumber(source.getWirePileLineNumber());
		target.setWireDeviceNumber(source.getWireDeviceNumber());
		target.setWireLineMisCode(source.getWireLineMisCode());
		target.setWireDeviceMisCode(source.getWireDeviceMisCode());

	}
	
	public String preUpdate() throws Exception {
		roomProject = roomProjectManager.get(projectId);
		List<Complaint> list = complaintManager.findComplaints(roomProject.getComplaint());
		fillComplaint(list);
		RoomProjectJson json = new RoomProjectJson();
		json.setComplaintRecords(complaintRecords);
		json.setRoomProject(convertRoomProject(roomProject));
		json.setTaskId(taskId);
		Gson gson = new Gson();
		this.printJson(gson.toJson(json));
		return null;
	}
	
	public void submitUpdate() throws Exception{
		RoomProject dbProject =  roomProjectManager.get(id);
		parseComplaints();
		roomProject.setComplaint(complaints);
		//dbProject.setArea(roomProject.getArea());
		ObjectConverter.convertCopy(roomProject, dbProject, RoomProject.class);
		//copyRoomProject(roomProject,dbProject);
		if (submit) {
			roomProjectFlowManager.process(taskId, dbProject, actionName, operateRemark);
		} else {
			roomProjectManager.update(dbProject);
		}
	}
	
	public void fillComplaint(List<Complaint> list){
        for (Complaint complaint : list) {
            complaintRecords.add(complaint);
        }
	}
	
    public Long getAttachmentId() {
		return attachmentId;
	}

	public void setAttachmentId(Long attachmentId) {
		this.attachmentId = attachmentId;
	}

	private String handleValue(@SuppressWarnings("rawtypes") Map map,String key){
		Object obj = map.get(key);
		if(obj != null){
			return obj.toString();
		}else{
			return "";
		}
	}
	
	@SuppressWarnings({"unchecked", "rawtypes"})
    public void parseComplaints() throws JSONException {
		StringBuffer sb = new StringBuffer();
		List<Map> list = (List<Map>) JSONUtil.deserialize(itemsJson);
        for (Map map : list) {
        	String complaintIdStr = handleValue(map,"id");
        	sb.append(complaintIdStr);
        	sb.append(",");
        }
        complaints = StringUtils.removeEnd(sb.toString(), ",");
        
    }

	public void setDataTypeStr(String dataTypeStr) {
		if (roomProject != null) {
			roomProject.setDataType(RoomDataType.valueOf(dataTypeStr));
		}
	}
	@JSON(serialize=false)
	public String getDataTypeStr() {
		if (roomProject != null) {
			if (roomProject.getDataType() != null) {
				return roomProject.getDataType().toString();
			}
		}
		return null;
	}
	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getProcessInstanceId() {
		return processInstanceId;
	}

	public void setProcessInstanceId(int processInstanceId) {
		this.processInstanceId = processInstanceId;
	}

	public int getTaskId() {
		return taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}
	
	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getBizNumber() {
		return bizNumber;
	}

	public void setBizNumber(String bizNumber) {
		this.bizNumber = bizNumber;
	}

	public String getOperateRemark() {
		return operateRemark;
	}

	public void setOperateRemark(String operateRemark) {
		this.operateRemark = operateRemark;
	}

	public RoomProject getRoomProject() {
		return roomProject;
	}

	public void setRoomProject(RoomProject roomProject) {
		this.roomProject = roomProject;
	}
	
	public String submitProject() throws Exception{
		parseComplaints();
		roomProject.setComplaint(complaints);
		/**
		 * 其它Term类似这样写法做个判断就可以避免错误：object references an unsaved transient instance - save the transien  instance before flushing
		 * 主要原因是因为struts2的prepare对象自动赋值导致gsmTerm对象自动关联上了Dictionary对象，并且Dictionary对象的Id=null;
		 */
//		if(roomProject.getGsmTerm()!=null && roomProject.getGsmTerm().getId()==null){
//			roomProject.setGsmTerm(null);
//		}
		roomProjectFlowManager.startRommProject(roomProject);
		return SUCCESS;
	}
	
	

	public String createProject() {
		bizNumber = SequenceGenerator.generate(SequenceType.XXXX);
		roomProject.setBizNumber(bizNumber);
		return "createProject";
	}

	public String toProcess() {
		task = taskManager.get(taskId);
		roomProject = roomProjectManager.getByProcessInstanceId(task.getProcessInstance().getId());
		return resultMap.get(task.getUnitName());
	}
	
	public String toView() {
		roomProject = roomProjectManager.getByProcessInstanceId(taskManager.get(taskId).getProcessInstance().getId());
		String unitName = taskManager.get(taskId).getUnitName();
		String complaintIds = roomProject.getComplaint();
		List<Long> idList = new ArrayList<Long>();
		List<Complaint> complaintList = new ArrayList<Complaint>();
		if(StringUtils.isNotBlank(complaintIds)){
			String[] strIds = complaintIds.split(",");
			for(String strId : strIds){
				idList.add(Long.valueOf(strId));
			}
		}
		if(idList.size() > 0){
			complaintList = complaintManager.findComplaintsByIDs(idList);
		}
		this.request.setAttribute("complaintList", complaintList);
		this.request.setAttribute("unitName", unitName);
		return "view";
	}
	
	public String view() {
		String id = this.request.getParameter("id");
		roomProject = roomProjectManager.get(Long.parseLong(id));
		String complaintIds = roomProject.getComplaint();
		List<Long> idList = new ArrayList<Long>();
		List<Complaint> complaintList = new ArrayList<Complaint>();
		if(StringUtils.isNotBlank(complaintIds)){
			String[] strIds = complaintIds.split(",");
			for(String strId : strIds){
				idList.add(Long.valueOf(strId));
			}
		}
		if(idList.size() > 0){
			complaintList = complaintManager.findComplaintsByIDs(idList);
		}
		this.request.setAttribute("complaintList", complaintList);
		return "view";
	}
	
	public String process() {
		if (attachmentFiles != null && !attachmentFiles.isEmpty()) {
			List<Attachment> attachs = new ArrayList<Attachment>();
			for (Attachment attachment : attachmentFiles) {
				if (attachment != null && attachment.getFile() != null) {
					attachment.setSize(attachment.getFile().length());
					attachment.setUploadTime(new Date());
					attachment.setUsername(SpringSecurityUtils.getCurrentUserName());
					attachment.setPath("room/" + roomProject.getId() + "/" + System.nanoTime() + "_" + attachment.getName());
					attachs.add(attachment);
				}
			}
			roomProject.getAttachments().addAll(attachs);
		}
		roomProjectFlowManager.process(taskId, roomProject, actionName, operateRemark);
		return SUCCESS;
	}
	
	public void removeAttachment() {
		roomProjectManager.removeAttachment(id, attachmentId);
	}
	
	private static final Map<String, String> resultMap = new HashMap<String, String>();
	
	private static final Map<Long,String> areaMap = new HashMap<Long,String>();
	
	private static final Map<String,String> areaTypeMap = new HashMap<String,String>();
	
	private static final Map<String,String> projectTypeMap = new HashMap<String,String>();
	
	static {
		resultMap.put("项目保留库", "xmblk");
		resultMap.put("投诉需求确认", "tsxqqr");
		resultMap.put("前期摸排测试", "qqmpcs");
		resultMap.put("网络部审核", "wlbsh");
		resultMap.put("项目疑难库", "xmynk");
		resultMap.put("分公司确认", "fgsqr");
		resultMap.put("网络部站号站名分配", "wlbzhzmfp");
		resultMap.put("工程部立项", "gcblx");
		resultMap.put("覆盖延伸方案设计", "fgysfasj");
		resultMap.put("覆盖延伸方案审核", "fgysfash");
		resultMap.put("覆盖延伸方案续审", "fgysfaxs");
		resultMap.put("传输方案设计", "csfasj");
		resultMap.put("传输方案审核", "csfash");
		resultMap.put("传输施工审核", "cssgsh");
		resultMap.put("提交土建图纸", "tjtjtz");
		resultMap.put("开工申请", "kgsq");

		resultMap.put("开工批复", "kgpf");
		resultMap.put("开工确认", "kgqr");
		resultMap.put("传输开工申请(分公司)", "cskgsq");
		resultMap.put("传输开工申请(有线室)", "cskgsq-yxs");
		resultMap.put("传输开工批复", "cskgpf");
		resultMap.put("传输物资配送", "cswzps");
		resultMap.put("传输物资到货", "cswzdh");
		resultMap.put("传输施工完工(分公司)", "cssgwg");
		resultMap.put("传输施工完工(有线室)", "cssgwg_yxs");
		resultMap.put("土建施工", "tjsg");
		resultMap.put("土建施工分公司", "tjsg_fgs");
		resultMap.put("土建完工", "tjwg");
		resultMap.put("土建完工分公司", "tjwg_fgs");
		resultMap.put("布线与设备施工", "bxysbsg");
		resultMap.put("布线与设备完工", "bxysbwg");
		resultMap.put("分制式站点开通", "fzszdkt");

		resultMap.put("GSM制式开通", "gsmzskt");
		resultMap.put("TD制式开通", "tdzskt");
		resultMap.put("WLAN制式开通", "wlanzskt");
		resultMap.put("LTE制式开通", "ltezskt");
		
		resultMap.put("网络部分制式审核", "wlbfzssh");
		resultMap.put("工程部提交验收", "gcbtjys");
		resultMap.put("完工验收", "wgys");
		resultMap.put("维护优化", "whyh");
		
		areaMap.put(new Long(2), "西湖");
		areaMap.put(new Long(3), "江干");
		areaMap.put(new Long(4), "武林");
		areaMap.put(new Long(5), "拱墅");
		areaMap.put(new Long(6), "滨江");
		areaMap.put(new Long(7), "下沙");
		areaMap.put(new Long(8), "余杭");
		areaMap.put(new Long(9), "富阳");
		areaMap.put(new Long(10), "萧山");
		areaMap.put(new Long(11), "桐庐");
		areaMap.put(new Long(12), "建德");
		areaMap.put(new Long(13), "淳安");
		areaMap.put(new Long(14), "临安");
		
		projectTypeMap.put("16", "解决VIP投诉");
		projectTypeMap.put("17", "新区跟进");
		projectTypeMap.put("18", "话务吸收");
		projectTypeMap.put("19", "深度覆盖");
		projectTypeMap.put("20", "解决普通投诉");
		projectTypeMap.put("21", "解决弱覆盖");
		projectTypeMap.put("22", "解决质差问题");
		projectTypeMap.put("23", "道路覆盖");
		projectTypeMap.put("24", "解决高层投诉");
		
		areaTypeMap.put("26", "普通小高层小区");
		areaTypeMap.put("27", "高档小高层小区");
		areaTypeMap.put("28", "普通高层小区");
		areaTypeMap.put("29", "高档高层小区");
		areaTypeMap.put("30", "大型商场");
		areaTypeMap.put("31", "中型商场");
		areaTypeMap.put("32", "小型商场");
		areaTypeMap.put("33", "室外商业街区");
		areaTypeMap.put("34", "大型办公楼");
		areaTypeMap.put("35", "中型办公楼");
		areaTypeMap.put("36", "普通办公楼");
		areaTypeMap.put("37", "小型办公楼");
		areaTypeMap.put("38", "五星级宾馆");
		areaTypeMap.put("39", "四星级宾馆");
		areaTypeMap.put("40", "三星级宾馆");
		areaTypeMap.put("41", "二星级及以下宾馆");
		areaTypeMap.put("42", "娱乐休闲场所");
		areaTypeMap.put("43", "地下空间休闲场所");
		areaTypeMap.put("44", "餐饮场所");
		areaTypeMap.put("45", "农居点");
		areaTypeMap.put("46", "工业园区");
		areaTypeMap.put("47", "校园");
		areaTypeMap.put("48", "隧道");
		areaTypeMap.put("49", "景区");
		areaTypeMap.put("50", "大型桥梁");
		areaTypeMap.put("51", "干扰区域");
		areaTypeMap.put("52", "行政村");
		areaTypeMap.put("53", "自然村");
		areaTypeMap.put("54", "车站码头");
		areaTypeMap.put("55", "专业市场");
		areaTypeMap.put("56", "多层");
		areaTypeMap.put("57", "电梯");
		areaTypeMap.put("58", "其他营业场所");
		areaTypeMap.put("59", "其他地下场所");
		areaTypeMap.put("60", "无需覆盖区域");
		areaTypeMap.put("61", "普通道路");
		areaTypeMap.put("62", "大型医院");
		areaTypeMap.put("63", "小型医院");
		areaTypeMap.put("64", "中型医院");
		areaTypeMap.put("65", "二级甲等以上");
		areaTypeMap.put("66", "二级甲等以下");
		
	}

	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()){
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		if (limit <= 0) {
			limit = 20;
		}
		page.setPageSize(limit);
		page.setPageNo((start / limit) + 1);
		page = roomProjectManager.findAllRoomProjects(page, filters);
		return "list";
	}
	
	public String getAllProject() throws Exception{
		User user=new User();
		User user2=new User();
		user=(User)request.getSession().getAttribute("user_profile");
		List<Dictionary> areaList=new ArrayList<Dictionary>();
		StringBuilder areas=new StringBuilder();
		if (limit <= 0) {
			limit = 20;
		}
		if(null!=user){
			//取出用户所在的区域的areaId拼接成字符串
			user2=userManager.findUserByLoginname(user.getLoginName());
			areaList=(List<Dictionary>)user2.getAreas();
			if(areaList.size()>0){
				for(Dictionary d : areaList){
					areas.append(d.getId());
					areas.append(",");
				}
				areas.deleteCharAt(areas.length()-1);
			}else{
				areas.append(0);
			}
		}else{
			areas.append(0);
		}
		System.out.println("所属区域："+areas.toString());
		Connection conn=null;
		CallableStatement cs=null;
		ResultSet rs=null;
		try{
			conn=datasource.getConnection();
			cs=conn.prepareCall("{call ROOMPROJECTRUNNING (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			if(null==rpt.getBizNumber() || "".equals(rpt.getBizNumber())){
				cs.setString(1, null);
			}else{
				cs.setString(1, rpt.getBizNumber());
			}
			if(null==rpt.getName() || "".equals(rpt.getName())){
				cs.setString(2, null);
			}else{
				cs.setString(2, rpt.getName());
			}
			if(null==rpt.getAddress() || "".equals(rpt.getAddress())){
				cs.setString(3, null);
			}else{
				cs.setString(3, rpt.getAddress());
			}
			if(null==rpt.getTelphone() || "".equals(rpt.getTelphone())){
				cs.setString(4, null);
			}else{
				cs.setString(4, rpt.getTelphone());
			}
			if(null==rpt.getLon() || "".equals(rpt.getLon())){
				cs.setString(5, null);
			}else{
				cs.setString(5, rpt.getLon());
			}
			if(null==rpt.getLat() || "".equals(rpt.getLat())){
				cs.setString(6, null);
			}else{
				cs.setString(6, rpt.getLat());
			}
			if(null==rpt.getSiteName() || "".equals(rpt.getSiteName())){
				cs.setString(7, null);
			}else{
				cs.setString(7, rpt.getSiteName());
			}
			if(null==rpt.getMainSignal() || "".equals(rpt.getMainSignal())){
				cs.setString(8, null);
			}else{
				cs.setString(8, rpt.getMainSignal());
			}
			if(null==rpt.getLiftSignal() || "".equals(rpt.getLiftSignal())){
				cs.setString(9, null);
			}else{
				cs.setString(9, rpt.getLiftSignal());
			}
			if(null==rpt.getLiftNumber() || rpt.getLiftNumber()==0){
				cs.setString(10, null);
			}else{
				cs.setString(10, rpt.getLiftNumber().toString());
			}
			if(null==rpt.getLiftTopFloor() || rpt.getLiftTopFloor()==0){
				cs.setString(11, null);
			}else{
				cs.setString(11, rpt.getLiftTopFloor().toString());
			}
			if(null==rpt.getUnderParkSignal() || "".equals(rpt.getUnderParkSignal())){
				cs.setString(12, null);
			}else{
				cs.setString(12, rpt.getUnderParkSignal());
			}
			if(null==rpt.getUnderParkNumber() || rpt.getUnderParkNumber()==0){
				cs.setString(13, null);
			}else{
				cs.setString(13, rpt.getUnderParkNumber().toString());
			}
			if(null==rpt.getUnderParkCarNumber() || rpt.getUnderParkCarNumber()==0){
				cs.setString(14, null);
			}else{
				cs.setString(14, rpt.getUnderParkCarNumber().toString());
			}
			if(null==rpt.getCoverArea() || rpt.getCoverArea()==0){
				cs.setString(15, null);
			}else{
				cs.setString(15, rpt.getCoverArea().toString());
			}
			if(null==rpt.getEmergencyLevel() || "".equals(rpt.getEmergencyLevel())){
				cs.setString(16, null);
			}else{
				cs.setString(16, rpt.getEmergencyLevel().toString());
			}
			if(null==rpt.getNetType() || "".equals(rpt.getNetType())){
				cs.setString(17, null);
			}else{
				cs.setString(17, rpt.getNetType());
			}
			if(null==rpt.getResolveContent() || "".equals(rpt.getResolveContent())){
				cs.setString(18, null);
			}else{
				cs.setString(18, rpt.getResolveContent());
			}
			if(null==rpt.getUnitName() || "".equals(rpt.getUnitName())){
				cs.setString(19, null);
			}else{
				cs.setString(19, rpt.getUnitName());
			}
			if(null==rpt.getStatus() || "".equals(rpt.getStatus())){
				cs.setString(20, "2");
			}else{
				cs.setString(20, rpt.getStatus());
			}
			if(null==rpt.getAreaId() || "".equals(rpt.getAreaId())){
				cs.setString(21, null);
			}else{
				cs.setString(21, rpt.getAreaId());
			}
			if(null==rpt.getAreaTypeId() || "".equals(rpt.getAreaTypeId())){
				cs.setString(22, null);
			}else{
				cs.setString(22, rpt.getAreaTypeId());
			}
			if(null==rpt.getProjectTypeId() || "".equals(rpt.getProjectTypeId())){
				cs.setString(23, null);
			}else{
				cs.setString(23, rpt.getProjectTypeId());
			}
			//默认根据用户拥有区域去获取数据
			cs.setString(24, areas.toString());
			if(null==rpt.getCloseTime()){
				cs.setString(25, null);
			}else{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				cs.setString(25, sdf.format(rpt.getCloseTime()));
			}
			//cs.setString(25, null);
			if (limit <= 0) {
				limit = 20;
			}
			cs.setInt(26, (start/limit)+1);
			cs.setInt(27, limit);
			cs.registerOutParameter(28, Type.INT);
			List<RoomProjectTask> rptList = new ArrayList<RoomProjectTask>();
			rptList.clear();
			rs=cs.executeQuery();
			while(rs.next()){
				RoomProjectTask rpTask = new RoomProjectTask();
				rpTask.setId(rs.getLong(1));
				rpTask.setAddress(rs.getString(2));
				rpTask.setAddressCode(rs.getString(3));
				rpTask.setBizNumber(rs.getString(4));
				rpTask.setBodyCareCost(rs.getString(5));
				rpTask.setBodyCareCostBelong(rs.getString(6));
				rpTask.setBodyCareFactory(rs.getString(7));
				rpTask.setBodyCareInvestBelong(rs.getString(8));
				rpTask.setBodyCareType(rs.getString(9));
				rpTask.setBsc(rs.getString(10));
				rpTask.setBuildType(rs.getString(11));
				rpTask.setCivilBuildUnit(rs.getString(12));
				rpTask.setCivilCost(rs.getFloat(13));
				rpTask.setCivilCostBelong(rs.getString(14));
				rpTask.setCivilInvestBelong(rs.getString(15));
				rpTask.setComplaint(rs.getString(16));
				rpTask.setConstructionCost(rs.getFloat(17));
				rpTask.setConstructionUnit(rs.getString(18));
				rpTask.setCostBelong(rs.getString(19));
				rpTask.setCoverArea(rs.getFloat(20));
				if(null==rs.getString(21)){
					rpTask.setCreateTime(null);
				}else{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					ParsePosition pos = new ParsePosition(0);
					Date d = sdf.parse(rs.getString(21),pos);
					rpTask.setCreateTime(d);
				}
				
				rpTask.setDataType(rs.getInt(22));
				rpTask.setEmergencyLevel(rs.getString(23));
				rpTask.setFgysBudgetTotal(rs.getFloat(24));
				rpTask.setFgysBuildConst(rs.getFloat(25));
				rpTask.setFgysBuildUnit(rs.getString(26));
				rpTask.setFgysCostBelong(rs.getString(27));
				rpTask.setFgysInvestBelong(rs.getString(28));
				rpTask.setFgysMisCode(rs.getString(29));
				rpTask.setGsmBsc(rs.getString(30));
				rpTask.setGsmBuildType(rs.getString(31));
				rpTask.setGsmSiteNumber(rs.getString(32));
				rpTask.setGsmTerm(rs.getString(33));
				rpTask.setHorsePowerMisCode(rs.getString(34));
				rpTask.setHorsePowerSuit(rs.getString(35));
				rpTask.setInvestBelong(rs.getString(36));
				rpTask.setInvestDispatchOrder(rs.getString(37));
				rpTask.setItemFinishedTime(rs.getString(38));
				rpTask.setLat(rs.getString(39));
				rpTask.setLiftNumber(rs.getInt(40));
				rpTask.setLiftSignal(rs.getString(41));
				rpTask.setLiftTopFloor(rs.getInt(42));
				rpTask.setLon(rs.getString(43));
				rpTask.setLteBsc(rs.getString(44));
				rpTask.setLteBuildType(rs.getString(45));
				rpTask.setLteSiteNumber(rs.getString(46));
				rpTask.setLteTerm(rs.getString(47));
				rpTask.setMainSignal(rs.getString(48));
				rpTask.setManufacturer(rs.getString(49));
				rpTask.setMaterialCost(rs.getFloat(50));
				rpTask.setName(rs.getString(51));
				rpTask.setNeedCivil(rs.getString(52));
				rpTask.setNetType(rs.getString(53));
				rpTask.setNewCreate(rs.getString(54));
				rpTask.setNewTransfer(rs.getInt(55));
				rpTask.setPath(rs.getString(56));
				rpTask.setPracticalEarly(rs.getInt(57));
				rpTask.setProcessInstanceId(rs.getInt(58));
				rpTask.setProjectTerm(rs.getString(59));
				rpTask.setRelatedCode(rs.getString(60));
				rpTask.setRemark(rs.getString(61));
				rpTask.setRepeaterBudgetTotal(rs.getFloat(62));
				rpTask.setRepeaterBuildCost(rs.getString(63));
				rpTask.setRepeaterBuildFactory(rs.getString(64));
				rpTask.setRepeaterCostBelong(rs.getString(65));
				rpTask.setRepeaterInvestBelong(rs.getString(66));
				rpTask.setRepeaterMaterial(rs.getString(67));
				rpTask.setRepeaterMisCode(rs.getString(68));
				if(null==rs.getString(69)){
					rpTask.setReplyDate(null);
				}else{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					ParsePosition pos = new ParsePosition(0);
					Date d = sdf.parse(rs.getString(69),pos);
					rpTask.setReplyDate(d);
				}
				
				rpTask.setResolveContent(rs.getString(70));
				rpTask.setResolveMethod(rs.getString(71));
				rpTask.setShareStatus(rs.getString(72));
				rpTask.setSiteAddress(rs.getString(73));
				rpTask.setSiteName(rs.getString(74));
				rpTask.setSiteNumber(rs.getString(75));
				rpTask.setTdBuildType(rs.getString(76));
				rpTask.setTdSiteNumber(rs.getString(77));
				rpTask.setTdTerm(rs.getString(78));
				rpTask.setTelphone(rs.getString(79));
				rpTask.setTransferBudget(rs.getFloat(80));
				rpTask.setTransferBuildscale(rs.getString(81));
				rpTask.setTransferConstructionUnit(rs.getString(82));
				rpTask.setTransferDesigner(rs.getString(83));
				rpTask.setTransferMaterialCost(rs.getString(84));
				rpTask.setTransferMisNumber(rs.getString(85));
				rpTask.setTransferOwnerMaterial(rs.getString(86));
				rpTask.setTransferPlanCompleteDate(rs.getString(87));
				rpTask.setTransferProjectBelong(rs.getString(88));
				rpTask.setTransferProjectName(rs.getString(89));
				rpTask.setTransferProjectNumber(rs.getString(90));
				rpTask.setTransferRemark(rs.getString(91));
				rpTask.setTransferStartApplyDate(rs.getString(92));
				rpTask.setTransferStartDate(rs.getString(93));
				rpTask.setTransferSupervisorUnit(rs.getString(94));
				rpTask.setUnderParkCarNumber(rs.getInt(95));
				rpTask.setUnderParkNumber(rs.getInt(96));
				rpTask.setUnderParkSignal(rs.getString(97));
				rpTask.setUploadType(rs.getString(98));
				rpTask.setWlanBuildType(rs.getString(99));
				rpTask.setWlanSiteNumber(rs.getString(100));
				rpTask.setWlanTerm(rs.getString(101));
				rpTask.setWorkMenuNumberOne(rs.getString(102));
				rpTask.setWorkMenuNumberTwo(rs.getString(103));
				rpTask.setYsType(rs.getString(104));
				rpTask.setZywNumber(rs.getString(105));
				rpTask.setAreaId(areaMap.get(Long.parseLong(rs.getString(106))));
				rpTask.setAreaTypeId(areaTypeMap.get(rs.getString(107)));
				rpTask.setFactoryId(rs.getString(108));
				rpTask.setProjectTypeId(projectTypeMap.get(rs.getString(109)));
				rpTask.setTermId(rs.getString(110));
				rpTask.setTdRnc(rs.getString(111));
				rpTask.setWlanAc(rs.getString(112));
				rpTask.setAllTransferMaterialCost(rs.getString(113));
				rpTask.setTransferMaterial(rs.getString(114));
				rpTask.setWireDeviceMisCode(rs.getString(115));
				rpTask.setWireDeviceNumber(rs.getString(116));
				rpTask.setWireLineMisCode(rs.getString(117));
				rpTask.setWirePileLineNumber(rs.getString(118));
				rpTask.setAction(rs.getString(119));
				rpTask.setActionStatus(rs.getString(120));
				if(null==rs.getString(121)){
					rpTask.setActivationTime(null);
				}else{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					ParsePosition pos = new ParsePosition(0);
					Date d = sdf.parse(rs.getString(121), pos);
					rpTask.setActivationTime(d);
				}
				rpTask.setAssigners(rs.getString(122));
				if(null==rs.getString(123)){
					rpTask.setCloseTime(null);
				}else{
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					ParsePosition pos = new ParsePosition(0);
					Date d = sdf.parse(rs.getString(123), pos);
					rpTask.setCloseTime(d);
				}
				rpTask.setDescription(rs.getString(124));
				rpTask.setOperator(rs.getString(125));
				rpTask.setStatus(rs.getString(126));
				rpTask.setUnitName(rs.getString(127));
				rpTask.setPreviousId(rs.getInt(128));
				rptList.add(rpTask);
			}
			totalCount=cs.getInt(28);
			rptPage.setTotalCount(totalCount);
			rptPage.setResult(rptList);
			//使用jackson把集合转换为JSON字符串
			//String rptListString=JsonBinder.buildNonDefaultBinder().toJson(rptList).toString();
			//this.setJsonString("{success:true,total:"+totalCount+",rows:"+rptListString+"}");
			rs.close();
			conn.close();
			//根据导出标记判断导出数据
			if(null!=this.export && rptList.size()>0 && rptList!=null){
				HSSFWorkbook workbook=this.getWorkbook(rptList);
				this.workbook2InputStream(workbook, "result");	
				return "excel";
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "projectlist";
	}

	@Override
	public void prepare() {
		if (id == null) {
			roomProject = new RoomProject();
		}else if(!update){
			roomProject = roomProjectManager.get(id);
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	@Override
	public RoomProject getModel() {
		return roomProject;
	}

	public String getComplaints() {
		return complaints;
	}

	public void setComplaints(String complaints) {
		this.complaints = complaints;
	}

	public Long getProjectId() {
		return projectId;
	}

	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	public List<Complaint> getComplaintRecords() {
		return complaintRecords;
	}

	public void setComplaintRecords(List<Complaint> complaintRecords) {
		this.complaintRecords = complaintRecords;
	}

	public Boolean getSubmit() {
		return submit;
	}

	public void setSubmit(Boolean submit) {
		this.submit = submit;
	}

	public Boolean getUpdate() {
		return update;
	}

	public void setUpdate(Boolean update) {
		this.update = update;
	}
	
	public String getProcessName() {
		return processName;
	}

	public void setProcessName(String processName) {
		this.processName = processName;
	}

	public String getActivityName() {
		return activityName;
	}

	public void setActivityName(String activityName) {
		this.activityName = activityName;
	}

	public String getProcessInstanceName() {
		return processInstanceName;
	}

	public void setProcessInstanceName(String processInstanceName) {
		this.processInstanceName = processInstanceName;
	}

	public String findRunningTask() {
		if(limit <= 0){
			limit = 10;
		}
		int pageNo = start/limit + 1;
		Page<Task> tasks = taskManager.findRunningTask(processName, activityName, 
				processInstanceId, pageNo, limit);
		taskResult = new Page<RoomTask>();
		taskResult.setPageNo(tasks.getPageNo());
		taskResult.setPageSize(tasks.getPageSize());
		taskResult.setTotalCount(tasks.getTotalCount());
		List<RoomTask> roomTasks = new ArrayList<RoomTask>();
		if (!tasks.getResult().isEmpty()) {
			for (Task task : tasks.getResult()) {
				RoomProject rp = roomProjectManager.getByProcessInstanceId(task.getProcessInstance().getId());
				roomTasks.add(new RoomTask(rp, task));
			}
		}
		taskResult.setResult(roomTasks);
		return "taskList";
	}
	
	public String findClosedTask() {
		if(limit <= 0){
			limit = 10;
		}
		int pageNo = start/limit + 1;
		Page<Task> tasks = taskManager.findClosedTask(processName, activityName, 
				processInstanceId, pageNo, limit);
		taskResult = new Page<RoomTask>();
		taskResult.setPageNo(tasks.getPageNo());
		taskResult.setPageSize(tasks.getPageSize());
		taskResult.setTotalCount(tasks.getTotalCount());
		List<RoomTask> roomTasks = new ArrayList<RoomTask>();
		if (!tasks.getResult().isEmpty()) {
			for (Task task : tasks.getResult()) {
				RoomProject rp = roomProjectManager.getByProcessInstanceId(task.getProcessInstance().getId());
				roomTasks.add(new RoomTask(rp, task));
			}
		}
		taskResult.setResult(roomTasks);
		return "closeTask";
	}
	
	public String removeProjectById()throws Exception{
		 boolean flag1 = taskManager.removeTask(processInstanceId);
		 boolean flag2 =  taskManager.removeProcessInstance(processInstanceId);
		 RoomProject roomProject = roomProjectManager.findRoomProject(processInstanceId);
		 List<Attachment> attachmentList = roomProject.getAttachments();
		 if(attachmentList != null && attachmentList.size() > 0){
			 for(Attachment attachment:attachmentList){
				 roomProjectManager.removeAttachment(attachment.getId());
			 }
		 }
		 boolean flag3 = roomProjectManager.removeProject(processInstanceId);
		 
		 if(flag1 && flag2 && flag3){
				rp.put("message", 1);
				rp.put("success", true);
		 }else{
				rp.put("message", 0);
				rp.put("success", false);
		 }
		 Gson gson = new Gson();
		 this.printJson(gson.toJson(rp));
		 return null;
	}
	
	public String isName() throws Exception{
		String name=request.getParameter("name");
		int count=roomProjectManager.getRoomProject(name);
		if(count>0){
			rp.put("message", 1);
			rp.put("success", true);
		}else{
			rp.put("message", 0);
			rp.put("success", false);
		}
		Gson gson = new Gson();
		this.printJson(gson.toJson(rp));
	
		return null;
	}
	//列标题
	final String[] columnNames = new String[]{"项目编号","项目名称","所属区域","区域性质","项目分类",
			"项目状态","地址","联系电话",
			"经度","纬度","站号","站名","是否新建","关联编码","主体信号","电梯信号","电梯数量",
			"电梯最高层数","地下车库信号","独立地下车库数量","地下车库车位数量","解决手段",
			"预计覆盖面积(m²)","紧急程度","网络类型","解决内容","建设类型","GSM站号","GSM建设类型",
			"GSM-BSC","GSM期数","TD站号","TD建设类型","TD-RNC","TD期数","WLAN站号","WLAN建设类型",
			"WLAN-AC","WLAN期数","LTE站号","LTE建设类型","LTE-BSC","LTE期数","直放站材料",
			"直放站施工费用","直放站施工厂家","动力配套","直放站预算总金额","开工申请",
			"前期落实","基站地址","是否土建","土建费用","土建施工单位","美化体类型","美化体施工单位",
			"美化体费用","传输是否新建","工单号一","工单号二","地址编码","投资派工单编号",
			"传输项目名称","传输项目编号","传输MIS编号","传输项目归属","传输建设规模","传输费用预算",
			"传输开工申请-甲供材料","开工申请-甲供材料","所有甲供材料费用","有线管线项目编号",
			"有线设备项目编号","有线管线MIS号","有线设备MIS号","甲供材料费用","覆盖延伸施工费用",
			"覆盖延伸预算总金额","覆盖延伸施工单位","项目完成时间","设计人员","传输施工单位",
			"监理单位","传输开工时间","传输预计竣工时间","传输开工申请时间","传输备注","投资归属",
			"土建投资归属","美化体投资归属","覆盖延伸投资归属","直放站投资归属","成本归属",
			"批复日期","覆盖延伸MIS任务号","直放站MIS任务号","动力配套MIS任务号"};
	
	public HSSFWorkbook getWorkbook(List<RoomProjectTask> list) throws Exception{
		//创建EXCEL表
		HSSFWorkbook workbook = new HSSFWorkbook();
		//创建工作薄
		HSSFSheet sheet = workbook.createSheet("sheet1");
		HSSFRow row = null; 
		HSSFCell cell = null;
		//创建第一行
		row = sheet.createRow(0);
		//创建行表头
		for(int i=0;i<columnNames.length;i++){
			cell=row.createCell(i);
			cell.setCellValue(columnNames[i]);
		}
		//把List中的数据导入表中
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				row=sheet.createRow(i+1);
				cell=row.createCell(0);
				cell.setCellValue(list.get(i).getBizNumber());
				cell=row.createCell(1);
				cell.setCellValue(list.get(i).getName());
				cell=row.createCell(2);
				//Long areaId=Long.parseLong(list.get(i).getAreaId());
				//cell.setCellValue(areaMap.get(areaId));
				cell.setCellValue(list.get(i).getAreaId());
				cell=row.createCell(3);
				//String areaType=list.get(i).getAreaTypeId();
				//cell.setCellValue(areaTypeMap.get(areaType));
				cell.setCellValue(list.get(i).getAreaTypeId());
				cell=row.createCell(4);
				//String projectType=list.get(i).getProjectTypeId();
				//cell.setCellValue(projectTypeMap.get(projectType));
				cell.setCellValue(list.get(i).getProjectTypeId());
				cell=row.createCell(5);
				cell.setCellValue(list.get(i).getUnitName());
				cell=row.createCell(6);
				cell.setCellValue(list.get(i).getAddress());
				cell=row.createCell(7);
				cell.setCellValue(list.get(i).getTelphone());
				cell=row.createCell(8);
				cell.setCellValue(list.get(i).getLon());
				cell=row.createCell(9);
				cell.setCellValue(list.get(i).getLat());
				cell=row.createCell(10);
				cell.setCellValue(list.get(i).getSiteNumber());
				cell=row.createCell(11);
				cell.setCellValue(list.get(i).getSiteName());
				cell=row.createCell(12);
				cell.setCellValue(list.get(i).getNewCreate());
				cell=row.createCell(13);
				cell.setCellValue(list.get(i).getRelatedCode());
				cell=row.createCell(14);
				cell.setCellValue(list.get(i).getMainSignal());
				cell=row.createCell(15);
				cell.setCellValue(list.get(i).getLiftSignal());
				cell=row.createCell(16);
				if(null==list.get(i).getLiftNumber()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getLiftNumber());
				}
				cell=row.createCell(17);
				if(null==list.get(i).getLiftTopFloor()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getLiftTopFloor());
				}
				cell=row.createCell(18);
				cell.setCellValue(list.get(i).getUnderParkSignal());
				cell=row.createCell(19);
				if(null==list.get(i).getUnderParkNumber()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getUnderParkNumber());
				}
				cell=row.createCell(20);
				if(null==list.get(i).getUnderParkCarNumber()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getUnderParkCarNumber());
				}
				
				cell=row.createCell(21);
				cell.setCellValue(list.get(i).getResolveMethod());
				cell=row.createCell(22);
				if(null==list.get(i).getCoverArea()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getCoverArea());
				}
				
				cell=row.createCell(23);
				cell.setCellValue(list.get(i).getEmergencyLevel());
				cell=row.createCell(24);
				cell.setCellValue(list.get(i).getNetType());
				cell=row.createCell(25);
				cell.setCellValue(list.get(i).getResolveContent());
				cell=row.createCell(26);
				cell.setCellValue(list.get(i).getBuildType());
				cell=row.createCell(27);
				cell.setCellValue(list.get(i).getGsmSiteNumber());
				cell=row.createCell(28);
				cell.setCellValue(list.get(i).getGsmBuildType());
				cell=row.createCell(29);
				cell.setCellValue(list.get(i).getGsmBsc());
				cell=row.createCell(30);
				cell.setCellValue(list.get(i).getGsmTerm());
				cell=row.createCell(31);
				cell.setCellValue(list.get(i).getTdSiteNumber());
				cell=row.createCell(32);
				cell.setCellValue(list.get(i).getTdBuildType());
				cell=row.createCell(33);
				cell.setCellValue(list.get(i).getTdRnc());
				cell=row.createCell(34);
				cell.setCellValue(list.get(i).getTdTerm());
				cell=row.createCell(35);
				cell.setCellValue(list.get(i).getWlanSiteNumber());
				cell=row.createCell(36);
				cell.setCellValue(list.get(i).getWlanBuildType());
				cell=row.createCell(37);
				cell.setCellValue(list.get(i).getWlanAc());
				cell=row.createCell(38);
				cell.setCellValue(list.get(i).getWlanTerm());
				cell=row.createCell(39);
				cell.setCellValue(list.get(i).getLteSiteNumber());
				cell=row.createCell(40);
				cell.setCellValue(list.get(i).getLteBuildType());
				cell=row.createCell(41);
				cell.setCellValue(list.get(i).getLteBsc());
				cell=row.createCell(42);
				cell.setCellValue(list.get(i).getLteTerm());
				cell=row.createCell(43);
				cell.setCellValue(list.get(i).getRepeaterMaterial());
				cell=row.createCell(44);
				cell.setCellValue(list.get(i).getRepeaterBuildCost());
				cell=row.createCell(45);
				cell.setCellValue(list.get(i).getRepeaterBuildFactory());
				cell=row.createCell(46);
				if(null==list.get(i).getHorsePowerSuit()){
					cell.setCellValue("");
				}else{
					if(list.get(i).getHorsePowerSuit().equals("true")){
						cell.setCellValue("是");
					}else{
						cell.setCellValue("否");
					}
				}
				cell=row.createCell(47);
				if(null==list.get(i).getRepeaterBudgetTotal()){
					cell.setCellValue(0.0);
				}else{
					cell.setCellValue(list.get(i).getRepeaterBudgetTotal());
				}
				cell=row.createCell(48);
				cell.setCellValue(list.get(i).getZywNumber());
				cell=row.createCell(49);
				if(list.get(i).getPracticalEarly()==0){
					cell.setCellValue("");
				}else{
					if(list.get(i).getPracticalEarly()==1){
						cell.setCellValue("是");
					}else{
						cell.setCellValue("否");
					}
				}
				
				cell=row.createCell(50);
				cell.setCellValue(list.get(i).getSiteAddress());
				cell=row.createCell(51);
				if(null==list.get(i).getNeedCivil()){
					cell.setCellValue("");
				}else{
					if(list.get(i).getNeedCivil().equals("true")){
						cell.setCellValue("是");
					}else{
						cell.setCellValue("否");
					}
				}
				cell=row.createCell(52);
				if(null==list.get(i).getCivilCost()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getCivilCost());
				}
				cell=row.createCell(53);
				cell.setCellValue(list.get(i).getCivilBuildUnit());
				cell=row.createCell(54);
				cell.setCellValue(list.get(i).getBodyCareType());
				cell=row.createCell(55);
				cell.setCellValue(list.get(i).getBodyCareFactory());
				cell=row.createCell(56);
				cell.setCellValue(list.get(i).getBodyCareCost());
				cell=row.createCell(57);
				cell.setCellValue(list.get(i).getNewTransfer());
				cell=row.createCell(58);
				cell.setCellValue(list.get(i).getWorkMenuNumberOne());
				cell=row.createCell(59);
				cell.setCellValue(list.get(i).getWorkMenuNumberTwo());
				cell=row.createCell(60);
				cell.setCellValue(list.get(i).getAddressCode());
				cell=row.createCell(61);
				cell.setCellValue(list.get(i).getInvestDispatchOrder());
				cell=row.createCell(62);
				cell.setCellValue(list.get(i).getTransferProjectName());
				cell=row.createCell(63);
				cell.setCellValue(list.get(i).getTransferProjectNumber());
				cell=row.createCell(64);
				cell.setCellValue(list.get(i).getTransferMisNumber());
				cell=row.createCell(65);
				cell.setCellValue(list.get(i).getTransferProjectBelong());
				cell=row.createCell(66);
				cell.setCellValue(list.get(i).getTransferBuildScale());
				cell=row.createCell(67);
				if(null==list.get(i).getTransferBudget()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getTransferBudget());
				}
				cell=row.createCell(68);
				cell.setCellValue(list.get(i).getTransferOwnerMaterial());
				cell=row.createCell(69);
				cell.setCellValue(list.get(i).getTransferMaterial());
				cell=row.createCell(70);
				cell.setCellValue(list.get(i).getAllTransferMaterialCost());
				cell=row.createCell(71);
				cell.setCellValue(list.get(i).getWirePileLineNumber());
				cell=row.createCell(72);
				cell.setCellValue(list.get(i).getWireDeviceNumber());
				cell=row.createCell(73);
				cell.setCellValue(list.get(i).getWireLineMisCode());
				cell=row.createCell(74);
				cell.setCellValue(list.get(i).getWireDeviceMisCode());
				cell=row.createCell(75);
				if(null==list.get(i).getTransferMaterialCost()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getTransferMaterialCost());
				}
				cell=row.createCell(76);
				if(null==list.get(i).getFgysBuildConst()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getFgysBuildConst());
				}
				cell=row.createCell(77);
				if(null==list.get(i).getFgysBudgetTotal()){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(list.get(i).getFgysBudgetTotal());
				}
				cell=row.createCell(78);
				cell.setCellValue(list.get(i).getFgysBuildUnit());
				cell=row.createCell(79);
				cell.setCellValue(list.get(i).getItemFinishedTime());
				cell=row.createCell(80);
				cell.setCellValue(list.get(i).getTransferDesigner());
				cell=row.createCell(81);
				cell.setCellValue(list.get(i).getTransferConstructionUnit());
				cell=row.createCell(82);
				cell.setCellValue(list.get(i).getTransferSupervisorUnit());
				cell=row.createCell(83);
				cell.setCellValue(list.get(i).getTransferStartDate());
				cell=row.createCell(84);
				cell.setCellValue(list.get(i).getTransferPlanCompleteDate());
				cell=row.createCell(85);
				cell.setCellValue(list.get(i).getTransferStartApplyDate());
				cell=row.createCell(86);
				cell.setCellValue(list.get(i).getTransferRemark());
				cell=row.createCell(87);
				cell.setCellValue(list.get(i).getInvestBelong());
				cell=row.createCell(88);
				cell.setCellValue(list.get(i).getCivilInvestBelong());
				cell=row.createCell(89);
				cell.setCellValue(list.get(i).getBodyCareInvestBelong());
				cell=row.createCell(90);
				cell.setCellValue(list.get(i).getFgysInvestBelong());
				cell=row.createCell(91);
				cell.setCellValue(list.get(i).getRepeaterInvestBelong());
				cell=row.createCell(92);
				cell.setCellValue(list.get(i).getCostBelong());
				cell=row.createCell(93);
				if(null==list.get(i).getReplyDate()){
					cell.setCellValue("");
				}else{
					SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
					cell.setCellValue(fmt.format(list.get(i).getReplyDate()));
				}
				cell=row.createCell(94);
				cell.setCellValue(list.get(i).getFgysMisCode());
				cell=row.createCell(95);
				cell.setCellValue(list.get(i).getRepeaterMisCode());
				cell=row.createCell(96);
				cell.setCellValue(list.get(i).getHorsePowerMisCode());
			}
		}
		
		return workbook;
	}
	
	//把excel写入流中 
	public void workbook2InputStream(HSSFWorkbook workbook, String fileName)   
		    throws Exception {  
		// 设置文件名(防止中文乱码)
		this.fileName = new String(fileName.getBytes(),"ISO8859-1");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();  
		try {
			workbook.write(baos); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		  
		baos.flush();   
		byte[] aa = baos.toByteArray();   
		this.excelStream = new ByteArrayInputStream(aa, 0, aa.length);  
		//this.excelStream = new ByteArrayInputStream(aa);
		baos.close();   
	}  
	
	public String doExport() throws Exception{
		/*List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()){
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		if (limit <= 0) {
			//通过每页要显示的行数控制要导出多少行
			limit = 30000;
		}
		page.setPageSize(limit);
		page.setPageNo((start / limit) + 1);
		page = roomProjectManager.findAllRoomProjects(page, filters);
		List<RoomProject> list = page.getResult();
		if(list.size()>0 || null!=list){
			HSSFWorkbook workbook=this.getWorkbook(list);
			this.workbook2InputStream(workbook, "result");
		}*/
		
		return "excel";
	}
	
	final String[] column_names = new String[]{"项目编号","项目名称","所属区域","项目地址","GSM期数","TD期数","WLAN期数","LTE期数",
			"GSM建设类型","TD建设类型","WLAN建设类型","LTE建设类型","GSM站号","TD站号","WLAN站号","LTE站号","前期落实","是否土建",
			"土建费用","土建施工单位","美化体类型","美化体施工单位","美化体费用","甲供材料费用","覆盖延伸施工单位","覆盖延伸施工费用",
			"覆盖延伸预算总金额","传输是否新建","动力是否配套","项目完成时间","直放站材料","直放站施工费用","直放站预算总金额","直放站施工单位",
			"投资工单号","成本工单号","地址编码","土建投资归属","美化体投资归属","覆盖延伸投资归属","直放站投资归属","直放站Mis任务号",
			"覆盖延伸Mis任务号","动力配套Mis任务号","批复日期"
	};
	
	public HSSFWorkbook getHSSFWorkbook(List<RoomProject> list) throws Exception{
		//创建EXCEL表
		HSSFWorkbook workbook = new HSSFWorkbook();
		//创建工作薄
		HSSFSheet sheet = workbook.createSheet("sheet1");
		HSSFRow row = null; 
		HSSFCell cell = null;
		//创建第一行
		row = sheet.createRow(0);
		//创建行表头
		for(int i=0;i<column_names.length;i++){
			cell=row.createCell(i);
			cell.setCellValue(column_names[i]);
		}
		//把List中的数据导入表中
		if(list != null && list.size() >0){
			int i = 0;
			for(RoomProject roomProject:list){
				i = i+1;
				row=sheet.createRow(i);
				cell=row.createCell(0);
				cell.setCellValue(roomProject.getBizNumber());
				cell=row.createCell(1);
				cell.setCellValue(roomProject.getName());
				cell=row.createCell(2);
				Long areaId = roomProject.getArea().getId();
				cell.setCellValue(areaMap.get(areaId));
				cell=row.createCell(3);
				cell.setCellValue(roomProject.getAddress());
				cell=row.createCell(4);
				cell.setCellValue(roomProject.getGsmTerm());
				cell=row.createCell(5);
				cell.setCellValue(roomProject.getTdTerm());
				cell=row.createCell(6);
				cell.setCellValue(roomProject.getWlanTerm());
				cell=row.createCell(7);
				cell.setCellValue(roomProject.getLteTerm());
				cell=row.createCell(8);
				cell.setCellValue(roomProject.getGsmBuildType());
				cell=row.createCell(9);
				cell.setCellValue(roomProject.getTdBuildType());
				cell=row.createCell(10);
				cell.setCellValue(roomProject.getWlanBuildType());
				cell=row.createCell(11);
				cell.setCellValue(roomProject.getLteBuildType());
				cell=row.createCell(12);
				cell.setCellValue(roomProject.getGsmSiteNumber());
				cell=row.createCell(13);
				cell.setCellValue(roomProject.getTdSiteNumber());
				cell=row.createCell(14);
				cell.setCellValue(roomProject.getWlanSiteNumber());
				cell=row.createCell(15);
				cell.setCellValue(roomProject.getLteSiteNumber());
				cell=row.createCell(16);
				boolean practicalEarly = roomProject.getPracticalEarly();
				if(practicalEarly){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");
				}
				cell=row.createCell(17);
				boolean needCivil = roomProject.getNeedCivil();
				if(needCivil){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");
				}
				cell=row.createCell(18);
				String civilCost = roomProject.getCivilCost();
				if(civilCost == null){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(civilCost);
				}
				cell=row.createCell(19);
				cell.setCellValue(roomProject.getCivilBuildUnit());
				cell=row.createCell(20);
				cell.setCellValue(roomProject.getBodycareType());
				cell=row.createCell(21);
				cell.setCellValue(roomProject.getBodycareFactory());
				cell=row.createCell(22);
				cell.setCellValue(roomProject.getBodycareCost());
				cell=row.createCell(23);
				String transferMaterialCost = roomProject.getTransferMaterialCost();
				if(transferMaterialCost == null){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(transferMaterialCost);
				}
				cell=row.createCell(24);
				cell.setCellValue(roomProject.getFgysBuildUnit());
				cell=row.createCell(25);
				String fgysBuildConst = roomProject.getFgysBuildConst();
				if(fgysBuildConst == null){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(fgysBuildConst);
				}
				cell=row.createCell(26);
				String fgysBudgetTotal = roomProject.getFgysBudgetTotal();
				if(fgysBudgetTotal == null){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(fgysBudgetTotal);
				}
				
				cell=row.createCell(27);
				boolean newTransfer = roomProject.getNewTransfer();
				if(newTransfer){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");
				}
				
				cell=row.createCell(28);
				boolean horsePowerSuit = Boolean.valueOf(roomProject.getHorsePowerSuit());
				if(horsePowerSuit){
					cell.setCellValue("是");
				}else{
					cell.setCellValue("否");				
				}
				
				cell=row.createCell(29);
				cell.setCellValue(roomProject.getItemFinishedTime());
				
				cell=row.createCell(30);
				cell.setCellValue(roomProject.getRepeaterMaterial());
				
				cell=row.createCell(31);
				cell.setCellValue(roomProject.getRepeaterBuildCost());
				
				
				cell=row.createCell(32);
				Float repeaterBudgetTotal = roomProject.getRepeaterBudgetTotal();
				if(repeaterBudgetTotal == null){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(repeaterBudgetTotal);
				}
				
				cell=row.createCell(33);
				cell.setCellValue(roomProject.getRepeaterBuildFactory());
				
				cell=row.createCell(34); 
				cell.setCellValue(roomProject.getWorkMenuNumberOne());
				cell=row.createCell(35);
				cell.setCellValue(roomProject.getWorkMenuNumberTwo());
				
				cell=row.createCell(36);
				cell.setCellValue(roomProject.getAddressCode());
				
				cell=row.createCell(37);
				cell.setCellValue(roomProject.getCivilInvestBelong());
				cell=row.createCell(38);
				cell.setCellValue(roomProject.getBodycareInvestBelong());
				cell=row.createCell(39);
				cell.setCellValue(roomProject.getFgysInvestBelong());
				cell=row.createCell(40);
				cell.setCellValue(roomProject.getRepeaterInvestBelong());
				cell=row.createCell(41);
				cell.setCellValue(roomProject.getRepeaterMisCode());
				cell=row.createCell(42);
				cell.setCellValue(roomProject.getFgysMisCode());
				cell=row.createCell(43);
				cell.setCellValue(roomProject.getHorsePowerMisCode());
				cell=row.createCell(44);
				Date replyDate = roomProject.getReplyDate();
				if(replyDate == null){
					cell.setCellValue(0);
				}else{
					cell.setCellValue(replyDate);
				}
			}
		}
		return workbook;
	}
	
	public String exportToExcelById()throws Exception{
		roomProject = roomProjectManager.findRoomProjectById(id);
		List<RoomProject> list = new ArrayList<RoomProject>();
		list.add(roomProject);
		HSSFWorkbook workbook=this.getHSSFWorkbook(list);
		this.workbook2InputStream(workbook, "kgpfresult");

		return "kgpfexcel";
	}
	
	/**
	 * 根据日期批量导出开发批复后的项目
	 * @return String
	 * @throws Exception
	 */
	public String exportToExcelByTime()throws Exception{
		
		String closedTime = this.getRequest().getParameter("closedtime");
		List<Task> taskList = this.taskManager.getTaskByClosedTime(closedTime);
		List<Integer> processInstanceIds = new ArrayList<Integer>();
		if(taskList != null && taskList.size() >0){
			for(Task task:taskList){
				processInstanceIds.add(task.getProcessInstance().getId());
			}
		}
		List<RoomProject> roomProjectList = null;
		if(processInstanceIds.size() >0){
			roomProjectList = roomProjectManager.getRoomProjectByProIds(processInstanceIds);
		}
	    if(roomProjectList != null && roomProjectList.size() >0){
	    	HSSFWorkbook workbook=this.getHSSFWorkbook(roomProjectList);
			this.workbook2InputStream(workbook, "kgpfresult");
			return "kgpfexcel";
	    }else{
	    	return ERROR;
	    }
		
	}
}
